package com.xx.test;

public class test {
	public static void main(String[] args) {
		int[] arr = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45 };
	       System.out.println(search(arr, 12));
	       System.out.println(search(arr, 45));
	       System.out.println(search(arr, 35));
	       System.out.println(search(arr, 89));
	       System.out.println(search(arr, 15));
	}
	public static int search(int[] arr, int key) {
	       int start = 0;
	       int end = arr.length - 1;
	       while (start <= end) {
	           int middle = (start + end) / 2;
	           if (key < arr[middle]) {
	               end = middle - 1;
	           } else if (key > arr[middle]) {
	               start = middle + 1;
	           } else {
	               return middle;
	           }
	       }
	       return -1;
	   }
}
